<!-- Copyright (c) 2014 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file. -->

<link rel="import" href="../common/cr-butterbar.html">
<link rel="import" href="../common/cr-button.html">
<link rel="import" href="../common/cr-dialog.html">
<link rel="import" href="../common/cr-toolbar.html">

<polymer-element name="cr-issue-revert-dialog" attributes="patchset">
    <template>
        <link rel="stylesheet" href="../common/common.css">
        <style>
            .dialog-fields-label {
                font-weight: bold;
                color: #959595;
                -webkit-user-select: none;
                cursor: default;
            }

            .dialog-fields-field,
            .dialog-fields-label {
                padding: 0.5em 16px;
            }

            .help-links {
                flex: 1;
                -webkit-flex: 1;
                display: -webkit-flex;
                display: flex;
                align-items: flex-end;
                -webkit-align-items: flex-end;
                flex-direction: column;
                -webkit-flex-direction: column;
            }
        </style>

        <dialog is="cr-dialog" id="dialog" on-cancel="{{ cancel }}">
            <cr-butterbar message="{{ butterbarMessage }}"></cr-butterbar>

            <h1>Revert patch</h1>

            <fieldset class="dialog-fields" disabled?="{{ sending }}">
                <label class="dialog-fields-label" for="reason">Reason</label>
                <div class="dialog-fields-field">
                    <textarea id="reason" value="{{ reason }}" rows="20"></textarea>
                </div>
            </fieldset>

            <cr-toolbar>
                <button is="cr-button" primary on-tap="{{ revert }}">Revert</button>
                <button is="cr-button" on-tap="{{ cancel }}">Cancel</button>
                <label>
                    <input type="checkbox" checked="{{ commit }}">
                    Automatically send revert to CQ
                </label>

                <div class="help-links">
                    <a href="https://sites.google.com/a/chromium.org/dev/developers/tree-sheriffs/sheriff-details-chromium?pli=1#TOC-Revert-Patchset-button-in-Rietveld" target="_blank">Revert help</a>
                </div>
            </cr-toolbar>
        </dialog>
    </template>
    <script>
        Polymer({
            created: function() {
                this.patchset = null;
                this.reason = "";
                this.commit = true;
                this.butterbarMessage = "";
                this.sending = false;
            },
            cancel: function(event) {
                this.close();
            },
            reset: function() {
                this.sending = false;
                this.butterbarMessage = "";
                this.reason = "";
                this.commit = true;
            },
            close: function() {
                this.reset();
                this.$.dialog.close();
            },
            revert: function() {
                var data = {
                    reason: this.reason,
                    commit: this.commit,
                };
                this.sending = true;
                this.butterbarMessage = "Saving...";
                this.patchset.revert(data)
                    .then(this.saveSuccess.bind(this))
                    .catch(this.saveFailure.bind(this));
            },
            saveSuccess: function() {
                this.fire("issue-refresh");
                this.close();
            },
            saveFailure: function(error) {
                // TODO(esprehn): We should show an better error message.
                this.sending = false;
                this.butterbarMessage = "Error: " + error.message;
            },
            showModal: function() {
                if (!this.patchset)
                    return;
                this.$.dialog.showModal();
            },
        });
    </script>
</polymer-element>
